﻿using Newtonsoft.Json.Linq;
using Sunny.UI;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Namer.Pages
{
    public class RandomNamePage
    {
        private static readonly List<Control> PageControls = new List<Control>();
        public RandomNamePage() => Interlize();

        public List<Control> GetControls(int x)
        {
            PageControls.Clear();
            Interlize();
            foreach (Control ctr in PageControls)
            {
                if(!ctr.Name.Contains("[noresize]")) ctr.Location = new Point(x + ctr.Location.X, ctr.Location.Y);
            }
            return PageControls;
        }
        private static UILabel Name;
        private static UIComboBox Class;
        private static void Interlize()
        {
            UIMarkLabel title = new UIMarkLabel()
            {
                Text = "随机点名",
                Location = new Point(10, 10)
            }; PageControls.Add(title);
            Name = new UILabel()
            {
                Location = new Point(10, 120),
                Width = UI.MainForm.Width - 70,
                Height = 45,
                TextAlign = ContentAlignment.MiddleCenter,
                Font = new Font("Microsoft YaHei", 18),
                Text = "请点击按钮抽取吧"
            };PageControls.Add(Name);
            UIButton GenRandom = new UIButton()
            {
                Text = "点一个!",
                Location = new Point(10, UI.MainForm.Height - 120),
                Size = new Size(UI.MainForm.Width - 70, 30)
            };PageControls.Add(GenRandom); GenRandom.Click += GenRandom_Click;
            Class = new UIComboBox()
            {
                Size = new Size(UI.MainForm.Width - 70, 30),
                Location = new Point(10, UI.MainForm.Height - 80)
            };PageControls.Add(Class);intersize();
        }

        private static void intersize()
        {
            Class.Items.Clear();
            foreach (string dara in BackGround.cm) Class.Items.Add(dara);
            if(Class.Items.Count > 0) Class.SelectedIndex = 0;
        }

        private static void GenRandom_Click(object sender, EventArgs e)
        {
            Random r1 = new Random();
            string[] data = BackGround.UsersList[Class.SelectedItem.ToString()];
            Name.Text = data[r1.Next(0, data.Count())];

        }
    }
    class BackGround
    {
        public static Dictionary<string, string[]> UsersList = new Dictionary<string, string[]>();

        public static readonly List<string> cm = new List<string>();

        private static string loccalbase = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\Namer\\";
        
        public static void Delete(string table)
        {
            File.Delete(loccalbase + table + ".json");
            OnDone(0);
        }

        public static void SaveToFile(string table,string newtxt,int count)
        {
            JObject root = JObject.Parse(File.ReadAllText(loccalbase + table + ".json"));
            JArray ary = JArray.Parse(root["users"].ToString());
            ary[count] = newtxt;
            root["users"] = ary;
            File.WriteAllText(loccalbase + table + ".json", root.ToString());
            OnDone(0);
        }

        public static void CreatNew(string table,string[] args)
        {
            JObject root = new JObject();
            JArray ary = new JArray();
            foreach (string data in args) ary.Add(data);
            root.Add("users",ary.ToString());
            File.WriteAllText(loccalbase + table + ".json",root.ToString());
            OnDone(0);
        }

        public static void AddToFileEnd(string table, string newtxt)
        {
            JObject root = JObject.Parse(File.ReadAllText(loccalbase + table + ".json"));
            JArray ary = JArray.Parse(root["users"].ToString());
            ary.Add(newtxt);
            root["users"] = ary;
            File.WriteAllText(loccalbase + table + ".json", root.ToString());
            OnDone(0);
        }
        
        public static void OnDone(int id)
        {
            cm.Clear();
            UsersList.Clear();
            if (!Directory.Exists(loccalbase)) Directory.CreateDirectory(loccalbase);
            DirectoryInfo d1 = new DirectoryInfo(loccalbase);
            FileInfo[] fs = d1.GetFiles();
            foreach (FileInfo cfg in fs)
            {
                JObject root = JObject.Parse(File.ReadAllText(cfg.FullName));
                List<string> Users = new List<string>();
                foreach (JValue data in JArray.Parse(root["users"].ToString()))
                {
                    Users.Add(data.ToString());
                }
                UsersList.Add(cfg.Name.Replace(".json", ""), Users.ToArray());
                cm.Add(cfg.Name.Replace(".json", ""));
                GC.Collect();
            }
        }
    }
}
